package com.xiaoma.future.completableFuture_start;

import com.xiaoma.future.SmallTool;

import java.util.concurrent.CompletableFuture;

public class ThenCompose {

    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白进入餐厅");
        SmallTool.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");

        long currentTimeMillis = System.currentTimeMillis();
        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("厨师炒菜");
            SmallTool.sleepMillis(2000);
            return "番茄炒蛋";
            //thenCompose 有依赖关系，传递到下一个stage，有返回值
        }).thenCompose(dish -> CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("服务员打饭");
            SmallTool.sleepMillis(2000);
            return dish + " + 米饭";
        }));

        SmallTool.printTimeAndThread("小白在打王者");
        SmallTool.printTimeAndThread(String.format("%s 好了,小白开吃", cf1.join()));
        System.out.println("耗时(s)："+(System.currentTimeMillis()-currentTimeMillis)/1000);
    }
}
